home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / info / lispref.info-10.z / lispref.info-10
Encoding:
GNU Info File  |  1998-05-21  |  48.9 KB  |  1,218 lines

  1. This is Info file ../../info/lispref.info, produced by Makeinfo version
  2. 1.68 from the input file lispref.texi.
  3.  
  4.    Edition History:
  5.  
  6.    GNU Emacs Lisp Reference Manual Second Edition (v2.01), May 1993 GNU
  7. Emacs Lisp Reference Manual Further Revised (v2.02), August 1993 Lucid
  8. Emacs Lisp Reference Manual (for 19.10) First Edition, March 1994
  9. XEmacs Lisp Programmer's Manual (for 19.12) Second Edition, April 1995
  10. GNU Emacs Lisp Reference Manual v2.4, June 1995 XEmacs Lisp
  11. Programmer's Manual (for 19.13) Third Edition, July 1995 XEmacs Lisp
  12. Reference Manual (for 19.14 and 20.0) v3.1, March 1996 XEmacs Lisp
  13. Reference Manual (for 19.15 and 20.1, 20.2) v3.2, April, May 1997
  14.  
  15.    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995 Free Software
  16. Foundation, Inc.  Copyright (C) 1994, 1995 Sun Microsystems, Inc.
  17. Copyright (C) 1995, 1996 Ben Wing.
  18.  
  19.    Permission is granted to make and distribute verbatim copies of this
  20. manual provided the copyright notice and this permission notice are
  21. preserved on all copies.
  22.  
  23.    Permission is granted to copy and distribute modified versions of
  24. this manual under the conditions for verbatim copying, provided that the
  25. entire resulting derived work is distributed under the terms of a
  26. permission notice identical to this one.
  27.  
  28.    Permission is granted to copy and distribute translations of this
  29. manual into another language, under the above conditions for modified
  30. versions, except that this permission notice may be stated in a
  31. translation approved by the Foundation.
  32.  
  33.    Permission is granted to copy and distribute modified versions of
  34. this manual under the conditions for verbatim copying, provided also
  35. that the section entitled "GNU General Public License" is included
  36. exactly as in the original, and provided that the entire resulting
  37. derived work is distributed under the terms of a permission notice
  38. identical to this one.
  39.  
  40.    Permission is granted to copy and distribute translations of this
  41. manual into another language, under the above conditions for modified
  42. versions, except that the section entitled "GNU General Public License"
  43. may be included in a translation approved by the Free Software
  44. Foundation instead of in the original English.
  45.  
  46. 
  47. File: lispref.info,  Node: Variable Aliases,  Prev: Buffer-Local Variables,  Up: Variables
  48.  
  49. Variable Aliases
  50. ================
  51.  
  52.    You can define a variable as an "alias" for another.  Any time you
  53. reference the former variable, the current value of the latter is
  54. returned.  Any time you change the value of the former variable, the
  55. value of the latter is actually changed.  This is useful in cases where
  56. you want to rename a variable but still make old code work (*note
  57. Obsoleteness::.).
  58.  
  59.  - Function: defvaralias VARIABLE ALIAS
  60.      This function defines VARIABLE as an alias for ALIAS.
  61.      Thenceforth, any operations performed on VARIABLE will actually be
  62.      performed on ALIAS.  Both VARIABLE and ALIAS should be symbols.
  63.      If ALIAS is `nil', remove any aliases for VARIABLE.  ALIAS can
  64.      itself be aliased, and the chain of variable aliases will be
  65.      followed appropriately.  If VARIABLE already has a value, this
  66.      value will be shadowed until the alias is removed, at which point
  67.      it will be restored.  Currently VARIABLE cannot be a built-in
  68.      variable, a variable that has a buffer-local value in any buffer,
  69.      or the symbols `nil' or `t'.
  70.  
  71.  - Function: variable-alias VARIABLE
  72.      If VARIABLE is aliased to another variable, this function returns
  73.      that variable.  VARIABLE should be a symbol.  If VARIABLE is not
  74.      aliased, this function returns `nil'.
  75.  
  76.  - Function: indirect-variable OBJECT
  77.      This function returns the variable at the end of OBJECT's
  78.      variable-alias chain.  If OBJECT is a symbol, follow all variable
  79.      aliases and return the final (non-aliased) symbol.  If OBJECT is
  80.      not a symbol, just return it.  Signal a
  81.      `cyclic-variable-indirection' error if there is a loop in the
  82.      variable chain of symbols.
  83.  
  84. 
  85. File: lispref.info,  Node: Functions,  Next: Macros,  Prev: Variables,  Up: Top
  86.  
  87. Functions
  88. *********
  89.  
  90.    A Lisp program is composed mainly of Lisp functions.  This chapter
  91. explains what functions are, how they accept arguments, and how to
  92. define them.
  93.  
  94. * Menu:
  95.  
  96. * What Is a Function::    Lisp functions vs. primitives; terminology.
  97. * Lambda Expressions::    How functions are expressed as Lisp objects.
  98. * Function Names::        A symbol can serve as the name of a function.
  99. * Defining Functions::    Lisp expressions for defining functions.
  100. * Calling Functions::     How to use an existing function.
  101. * Mapping Functions::     Applying a function to each element of a list, etc.
  102. * Anonymous Functions::   Lambda expressions are functions with no names.
  103. * Function Cells::        Accessing or setting the function definition
  104.                             of a symbol.
  105. * Inline Functions::      Defining functions that the compiler will open code.
  106. * Related Topics::        Cross-references to specific Lisp primitives
  107.                             that have a special bearing on how functions work.
  108.  
  109. 
  110. File: lispref.info,  Node: What Is a Function,  Next: Lambda Expressions,  Up: Functions
  111.  
  112. What Is a Function?
  113. ===================
  114.  
  115.    In a general sense, a function is a rule for carrying on a
  116. computation given several values called "arguments".  The result of the
  117. computation is called the value of the function.  The computation can
  118. also have side effects: lasting changes in the values of variables or
  119. the contents of data structures.
  120.  
  121.    Here are important terms for functions in XEmacs Lisp and for other
  122. function-like objects.
  123.  
  124. "function"
  125.      In XEmacs Lisp, a "function" is anything that can be applied to
  126.      arguments in a Lisp program.  In some cases, we use it more
  127.      specifically to mean a function written in Lisp.  Special forms and
  128.      macros are not functions.
  129.  
  130. "primitive"
  131.      A "primitive" is a function callable from Lisp that is written in
  132.      C, such as `car' or `append'.  These functions are also called
  133.      "built-in" functions or "subrs".  (Special forms are also
  134.      considered primitives.)
  135.  
  136.      Usually the reason that a function is a primitives is because it is
  137.      fundamental, because it provides a low-level interface to operating
  138.      system services, or because it needs to run fast.  Primitives can
  139.      be modified or added only by changing the C sources and
  140.      recompiling the editor.  See *Note Writing Lisp Primitives:
  141.      (internals)Writing Lisp Primitives.
  142.  
  143. "lambda expression"
  144.      A "lambda expression" is a function written in Lisp.  These are
  145.      described in the following section.  *Note Lambda Expressions::.
  146.  
  147. "special form"
  148.      A "special form" is a primitive that is like a function but does
  149.      not evaluate all of its arguments in the usual way.  It may
  150.      evaluate only some of the arguments, or may evaluate them in an
  151.      unusual order, or several times.  Many special forms are described
  152.      in *Note Control Structures::.
  153.  
  154. "macro"
  155.      A "macro" is a construct defined in Lisp by the programmer.  It
  156.      differs from a function in that it translates a Lisp expression
  157.      that you write into an equivalent expression to be evaluated
  158.      instead of the original expression.  Macros enable Lisp
  159.      programmers to do the sorts of things that special forms can do.
  160.      *Note Macros::, for how to define and use macros.
  161.  
  162. "command"
  163.      A "command" is an object that `command-execute' can invoke; it is
  164.      a possible definition for a key sequence.  Some functions are
  165.      commands; a function written in Lisp is a command if it contains an
  166.      interactive declaration (*note Defining Commands::.).  Such a
  167.      function can be called from Lisp expressions like other functions;
  168.      in this case, the fact that the function is a command makes no
  169.      difference.
  170.  
  171.      Keyboard macros (strings and vectors) are commands also, even
  172.      though they are not functions.  A symbol is a command if its
  173.      function definition is a command; such symbols can be invoked with
  174.      `M-x'.  The symbol is a function as well if the definition is a
  175.      function.  *Note Command Overview::.
  176.  
  177. "keystroke command"
  178.      A "keystroke command" is a command that is bound to a key sequence
  179.      (typically one to three keystrokes).  The distinction is made here
  180.      merely to avoid confusion with the meaning of "command" in
  181.      non-Emacs editors; for Lisp programs, the distinction is normally
  182.      unimportant.
  183.  
  184. "compiled function"
  185.      A "compiled function" is a function that has been compiled by the
  186.      byte compiler.  *Note Compiled-Function Type::.
  187.  
  188.  - Function: subrp OBJECT
  189.      This function returns `t' if OBJECT is a built-in function (i.e.,
  190.      a Lisp primitive).
  191.  
  192.           (subrp 'message)            ; `message' is a symbol,
  193.                => nil                 ;   not a subr object.
  194.           (subrp (symbol-function 'message))
  195.                => t
  196.  
  197.  - Function: compiled-function-p OBJECT
  198.      This function returns `t' if OBJECT is a compiled function.  For
  199.      example:
  200.  
  201.           (compiled-function-p (symbol-function 'next-line))
  202.                => t
  203.  
  204. 
  205. File: lispref.info,  Node: Lambda Expressions,  Next: Function Names,  Prev: What Is a Function,  Up: Functions
  206.  
  207. Lambda Expressions
  208. ==================
  209.  
  210.    A function written in Lisp is a list that looks like this:
  211.  
  212.      (lambda (ARG-VARIABLES...)
  213.        [DOCUMENTATION-STRING]
  214.        [INTERACTIVE-DECLARATION]
  215.        BODY-FORMS...)
  216.  
  217. Such a list is called a "lambda expression".  In XEmacs Lisp, it
  218. actually is valid as an expression--it evaluates to itself.  In some
  219. other Lisp dialects, a lambda expression is not a valid expression at
  220. all.  In either case, its main use is not to be evaluated as an
  221. expression, but to be called as a function.
  222.  
  223. * Menu:
  224.  
  225. * Lambda Components::       The parts of a lambda expression.
  226. * Simple Lambda::           A simple example.
  227. * Argument List::           Details and special features of argument lists.
  228. * Function Documentation::  How to put documentation in a function.
  229.  
  230. 
  231. File: lispref.info,  Node: Lambda Components,  Next: Simple Lambda,  Up: Lambda Expressions
  232.  
  233. Components of a Lambda Expression
  234. ---------------------------------
  235.  
  236.    A function written in Lisp (a "lambda expression") is a list that
  237. looks like this:
  238.  
  239.      (lambda (ARG-VARIABLES...)
  240.        [DOCUMENTATION-STRING]
  241.        [INTERACTIVE-DECLARATION]
  242.        BODY-FORMS...)
  243.  
  244.    The first element of a lambda expression is always the symbol
  245. `lambda'.  This indicates that the list represents a function.  The
  246. reason functions are defined to start with `lambda' is so that other
  247. lists, intended for other uses, will not accidentally be valid as
  248. functions.
  249.  
  250.    The second element is a list of symbols-the argument variable names.
  251. This is called the "lambda list".  When a Lisp function is called, the
  252. argument values are matched up against the variables in the lambda
  253. list, which are given local bindings with the values provided.  *Note
  254. Local Variables::.
  255.  
  256.    The documentation string is a Lisp string object placed within the
  257. function definition to describe the function for the XEmacs help
  258. facilities.  *Note Function Documentation::.
  259.  
  260.    The interactive declaration is a list of the form `(interactive
  261. CODE-STRING)'.  This declares how to provide arguments if the function
  262. is used interactively.  Functions with this declaration are called
  263. "commands"; they can be called using `M-x' or bound to a key.
  264. Functions not intended to be called in this way should not have
  265. interactive declarations.  *Note Defining Commands::, for how to write
  266. an interactive declaration.
  267.  
  268.    The rest of the elements are the "body" of the function: the Lisp
  269. code to do the work of the function (or, as a Lisp programmer would say,
  270. "a list of Lisp forms to evaluate").  The value returned by the
  271. function is the value returned by the last element of the body.
  272.  
  273. 
  274. File: lispref.info,  Node: Simple Lambda,  Next: Argument List,  Prev: Lambda Components,  Up: Lambda Expressions
  275.  
  276. A Simple Lambda-Expression Example
  277. ----------------------------------
  278.  
  279.    Consider for example the following function:
  280.  
  281.      (lambda (a b c) (+ a b c))
  282.  
  283. We can call this function by writing it as the CAR of an expression,
  284. like this:
  285.  
  286.      ((lambda (a b c) (+ a b c))
  287.       1 2 3)
  288.  
  289. This call evaluates the body of the lambda expression  with the variable
  290. `a' bound to 1, `b' bound to 2, and `c' bound to 3.  Evaluation of the
  291. body adds these three numbers, producing the result 6; therefore, this
  292. call to the function returns the value 6.
  293.  
  294.    Note that the arguments can be the results of other function calls,
  295. as in this example:
  296.  
  297.      ((lambda (a b c) (+ a b c))
  298.       1 (* 2 3) (- 5 4))
  299.  
  300. This evaluates the arguments `1', `(* 2 3)', and `(- 5 4)' from left to
  301. right.  Then it applies the lambda expression to the argument values 1,
  302. 6 and 1 to produce the value 8.
  303.  
  304.    It is not often useful to write a lambda expression as the CAR of a
  305. form in this way.  You can get the same result, of making local
  306. variables and giving them values, using the special form `let' (*note
  307. Local Variables::.).  And `let' is clearer and easier to use.  In
  308. practice, lambda expressions are either stored as the function
  309. definitions of symbols, to produce named functions, or passed as
  310. arguments to other functions (*note Anonymous Functions::.).
  311.  
  312.    However, calls to explicit lambda expressions were very useful in the
  313. old days of Lisp, before the special form `let' was invented.  At that
  314. time, they were the only way to bind and initialize local variables.
  315.  
  316. 
  317. File: lispref.info,  Node: Argument List,  Next: Function Documentation,  Prev: Simple Lambda,  Up: Lambda Expressions
  318.  
  319. Advanced Features of Argument Lists
  320. -----------------------------------
  321.  
  322.    Our simple sample function, `(lambda (a b c) (+ a b c))', specifies
  323. three argument variables, so it must be called with three arguments: if
  324. you try to call it with only two arguments or four arguments, you get a
  325. `wrong-number-of-arguments' error.
  326.  
  327.    It is often convenient to write a function that allows certain
  328. arguments to be omitted.  For example, the function `substring' accepts
  329. three arguments--a string, the start index and the end index--but the
  330. third argument defaults to the LENGTH of the string if you omit it.  It
  331. is also convenient for certain functions to accept an indefinite number
  332. of arguments, as the functions `list' and `+' do.
  333.  
  334.    To specify optional arguments that may be omitted when a function is
  335. called, simply include the keyword `&optional' before the optional
  336. arguments.  To specify a list of zero or more extra arguments, include
  337. the keyword `&rest' before one final argument.
  338.  
  339.    Thus, the complete syntax for an argument list is as follows:
  340.  
  341.      (REQUIRED-VARS...
  342.       [&optional OPTIONAL-VARS...]
  343.       [&rest REST-VAR])
  344.  
  345. The square brackets indicate that the `&optional' and `&rest' clauses,
  346. and the variables that follow them, are optional.
  347.  
  348.    A call to the function requires one actual argument for each of the
  349. REQUIRED-VARS.  There may be actual arguments for zero or more of the
  350. OPTIONAL-VARS, and there cannot be any actual arguments beyond that
  351. unless the lambda list uses `&rest'.  In that case, there may be any
  352. number of extra actual arguments.
  353.  
  354.    If actual arguments for the optional and rest variables are omitted,
  355. then they always default to `nil'.  There is no way for the function to
  356. distinguish between an explicit argument of `nil' and an omitted
  357. argument.  However, the body of the function is free to consider `nil'
  358. an abbreviation for some other meaningful value.  This is what
  359. `substring' does; `nil' as the third argument to `substring' means to
  360. use the length of the string supplied.
  361.  
  362.      Common Lisp note: Common Lisp allows the function to specify what
  363.      default value to use when an optional argument is omitted; XEmacs
  364.      Lisp always uses `nil'.
  365.  
  366.    For example, an argument list that looks like this:
  367.  
  368.      (a b &optional c d &rest e)
  369.  
  370. binds `a' and `b' to the first two actual arguments, which are
  371. required.  If one or two more arguments are provided, `c' and `d' are
  372. bound to them respectively; any arguments after the first four are
  373. collected into a list and `e' is bound to that list.  If there are only
  374. two arguments, `c' is `nil'; if two or three arguments, `d' is `nil';
  375. if four arguments or fewer, `e' is `nil'.
  376.  
  377.    There is no way to have required arguments following optional
  378. ones--it would not make sense.  To see why this must be so, suppose
  379. that `c' in the example were optional and `d' were required.  Suppose
  380. three actual arguments are given; which variable would the third
  381. argument be for?  Similarly, it makes no sense to have any more
  382. arguments (either required or optional) after a `&rest' argument.
  383.  
  384.    Here are some examples of argument lists and proper calls:
  385.  
  386.      ((lambda (n) (1+ n))                ; One required:
  387.       1)                                 ; requires exactly one argument.
  388.           => 2
  389.      ((lambda (n &optional n1)           ; One required and one optional:
  390.               (if n1 (+ n n1) (1+ n)))   ; 1 or 2 arguments.
  391.       1 2)
  392.           => 3
  393.      ((lambda (n &rest ns)               ; One required and one rest:
  394.               (+ n (apply '+ ns)))       ; 1 or more arguments.
  395.       1 2 3 4 5)
  396.           => 15
  397.  
  398. 
  399. File: lispref.info,  Node: Function Documentation,  Prev: Argument List,  Up: Lambda Expressions
  400.  
  401. Documentation Strings of Functions
  402. ----------------------------------
  403.  
  404.    A lambda expression may optionally have a "documentation string" just
  405. after the lambda list.  This string does not affect execution of the
  406. function; it is a kind of comment, but a systematized comment which
  407. actually appears inside the Lisp world and can be used by the XEmacs
  408. help facilities.  *Note Documentation::, for how the
  409. DOCUMENTATION-STRING is accessed.
  410.  
  411.    It is a good idea to provide documentation strings for all the
  412. functions in your program, even those that are only called from within
  413. your program.  Documentation strings are like comments, except that they
  414. are easier to access.
  415.  
  416.    The first line of the documentation string should stand on its own,
  417. because `apropos' displays just this first line.  It should consist of
  418. one or two complete sentences that summarize the function's purpose.
  419.  
  420.    The start of the documentation string is usually indented in the
  421. source file, but since these spaces come before the starting
  422. double-quote, they are not part of the string.  Some people make a
  423. practice of indenting any additional lines of the string so that the
  424. text lines up in the program source.  *This is a mistake.*  The
  425. indentation of the following lines is inside the string; what looks
  426. nice in the source code will look ugly when displayed by the help
  427. commands.
  428.  
  429.    You may wonder how the documentation string could be optional, since
  430. there are required components of the function that follow it (the body).
  431. Since evaluation of a string returns that string, without any side
  432. effects, it has no effect if it is not the last form in the body.
  433. Thus, in practice, there is no confusion between the first form of the
  434. body and the documentation string; if the only body form is a string
  435. then it serves both as the return value and as the documentation.
  436.  
  437. 
  438. File: lispref.info,  Node: Function Names,  Next: Defining Functions,  Prev: Lambda Expressions,  Up: Functions
  439.  
  440. Naming a Function
  441. =================
  442.  
  443.    In most computer languages, every function has a name; the idea of a
  444. function without a name is nonsensical.  In Lisp, a function in the
  445. strictest sense has no name.  It is simply a list whose first element is
  446. `lambda', or a primitive subr-object.
  447.  
  448.    However, a symbol can serve as the name of a function.  This happens
  449. when you put the function in the symbol's "function cell" (*note Symbol
  450. Components::.).  Then the symbol itself becomes a valid, callable
  451. function, equivalent to the list or subr-object that its function cell
  452. refers to.  The contents of the function cell are also called the
  453. symbol's "function definition".  The procedure of using a symbol's
  454. function definition in place of the symbol is called "symbol function
  455. indirection"; see *Note Function Indirection::.
  456.  
  457.    In practice, nearly all functions are given names in this way and
  458. referred to through their names.  For example, the symbol `car' works
  459. as a function and does what it does because the primitive subr-object
  460. `#<subr car>' is stored in its function cell.
  461.  
  462.    We give functions names because it is convenient to refer to them by
  463. their names in Lisp expressions.  For primitive subr-objects such as
  464. `#<subr car>', names are the only way you can refer to them: there is
  465. no read syntax for such objects.  For functions written in Lisp, the
  466. name is more convenient to use in a call than an explicit lambda
  467. expression.  Also, a function with a name can refer to itself--it can
  468. be recursive.  Writing the function's name in its own definition is much
  469. more convenient than making the function definition point to itself
  470. (something that is not impossible but that has various disadvantages in
  471. practice).
  472.  
  473.    We often identify functions with the symbols used to name them.  For
  474. example, we often speak of "the function `car'", not distinguishing
  475. between the symbol `car' and the primitive subr-object that is its
  476. function definition.  For most purposes, there is no need to
  477. distinguish.
  478.  
  479.    Even so, keep in mind that a function need not have a unique name.
  480. While a given function object *usually* appears in the function cell of
  481. only one symbol, this is just a matter of convenience.  It is easy to
  482. store it in several symbols using `fset'; then each of the symbols is
  483. equally well a name for the same function.
  484.  
  485.    A symbol used as a function name may also be used as a variable;
  486. these two uses of a symbol are independent and do not conflict.
  487.  
  488. 
  489. File: lispref.info,  Node: Defining Functions,  Next: Calling Functions,  Prev: Function Names,  Up: Functions
  490.  
  491. Defining Functions
  492. ==================
  493.  
  494.    We usually give a name to a function when it is first created.  This
  495. is called "defining a function", and it is done with the `defun'
  496. special form.
  497.  
  498.  - Special Form: defun NAME ARGUMENT-LIST BODY-FORMS
  499.      `defun' is the usual way to define new Lisp functions.  It defines
  500.      the symbol NAME as a function that looks like this:
  501.  
  502.           (lambda ARGUMENT-LIST . BODY-FORMS)
  503.  
  504.      `defun' stores this lambda expression in the function cell of
  505.      NAME.  It returns the value NAME, but usually we ignore this value.
  506.  
  507.      As described previously (*note Lambda Expressions::.),
  508.      ARGUMENT-LIST is a list of argument names and may include the
  509.      keywords `&optional' and `&rest'.  Also, the first two forms in
  510.      BODY-FORMS may be a documentation string and an interactive
  511.      declaration.
  512.  
  513.      There is no conflict if the same symbol NAME is also used as a
  514.      variable, since the symbol's value cell is independent of the
  515.      function cell.  *Note Symbol Components::.
  516.  
  517.      Here are some examples:
  518.  
  519.           (defun foo () 5)
  520.                => foo
  521.           (foo)
  522.                => 5
  523.           
  524.           (defun bar (a &optional b &rest c)
  525.               (list a b c))
  526.                => bar
  527.           (bar 1 2 3 4 5)
  528.                => (1 2 (3 4 5))
  529.           (bar 1)
  530.                => (1 nil nil)
  531.           (bar)
  532.           error--> Wrong number of arguments.
  533.           
  534.           (defun capitalize-backwards ()
  535.             "Upcase the last letter of a word."
  536.             (interactive)
  537.             (backward-word 1)
  538.             (forward-word 1)
  539.             (backward-char 1)
  540.             (capitalize-word 1))
  541.                => capitalize-backwards
  542.  
  543.      Be careful not to redefine existing functions unintentionally.
  544.      `defun' redefines even primitive functions such as `car' without
  545.      any hesitation or notification.  Redefining a function already
  546.      defined is often done deliberately, and there is no way to
  547.      distinguish deliberate redefinition from unintentional
  548.      redefinition.
  549.  
  550.  - Function: define-function NAME DEFINITION
  551.  - Function: defalias NAME DEFINITION
  552.      These equivalent special forms define the symbol NAME as a
  553.      function, with definition DEFINITION (which can be any valid Lisp
  554.      function).
  555.  
  556.      The proper place to use `define-function' or `defalias' is where a
  557.      specific function name is being defined--especially where that
  558.      name appears explicitly in the source file being loaded.  This is
  559.      because `define-function' and `defalias' record which file defined
  560.      the function, just like `defun'.  (*note Unloading::.).
  561.  
  562.      By contrast, in programs that manipulate function definitions for
  563.      other purposes, it is better to use `fset', which does not keep
  564.      such records.
  565.  
  566.    See also `defsubst', which defines a function like `defun' and tells
  567. the Lisp compiler to open-code it.  *Note Inline Functions::.
  568.  
  569. 
  570. File: lispref.info,  Node: Calling Functions,  Next: Mapping Functions,  Prev: Defining Functions,  Up: Functions
  571.  
  572. Calling Functions
  573. =================
  574.  
  575.    Defining functions is only half the battle.  Functions don't do
  576. anything until you "call" them, i.e., tell them to run.  Calling a
  577. function is also known as "invocation".
  578.  
  579.    The most common way of invoking a function is by evaluating a list.
  580. For example, evaluating the list `(concat "a" "b")' calls the function
  581. `concat' with arguments `"a"' and `"b"'.  *Note Evaluation::, for a
  582. description of evaluation.
  583.  
  584.    When you write a list as an expression in your program, the function
  585. name is part of the program.  This means that you choose which function
  586. to call, and how many arguments to give it, when you write the program.
  587. Usually that's just what you want.  Occasionally you need to decide at
  588. run time which function to call.  To do that, use the functions
  589. `funcall' and `apply'.
  590.  
  591.  - Function: funcall FUNCTION &rest ARGUMENTS
  592.      `funcall' calls FUNCTION with ARGUMENTS, and returns whatever
  593.      FUNCTION returns.
  594.  
  595.      Since `funcall' is a function, all of its arguments, including
  596.      FUNCTION, are evaluated before `funcall' is called.  This means
  597.      that you can use any expression to obtain the function to be
  598.      called.  It also means that `funcall' does not see the expressions
  599.      you write for the ARGUMENTS, only their values.  These values are
  600.      *not* evaluated a second time in the act of calling FUNCTION;
  601.      `funcall' enters the normal procedure for calling a function at the
  602.      place where the arguments have already been evaluated.
  603.  
  604.      The argument FUNCTION must be either a Lisp function or a
  605.      primitive function.  Special forms and macros are not allowed,
  606.      because they make sense only when given the "unevaluated" argument
  607.      expressions.  `funcall' cannot provide these because, as we saw
  608.      above, it never knows them in the first place.
  609.  
  610.           (setq f 'list)
  611.                => list
  612.           (funcall f 'x 'y 'z)
  613.                => (x y z)
  614.           (funcall f 'x 'y '(z))
  615.                => (x y (z))
  616.           (funcall 'and t nil)
  617.           error--> Invalid function: #<subr and>
  618.  
  619.      Compare these example with the examples of `apply'.
  620.  
  621.  - Function: apply FUNCTION &rest ARGUMENTS
  622.      `apply' calls FUNCTION with ARGUMENTS, just like `funcall' but
  623.      with one difference: the last of ARGUMENTS is a list of arguments
  624.      to give to FUNCTION, rather than a single argument.  We also say
  625.      that `apply' "spreads" this list so that each individual element
  626.      becomes an argument.
  627.  
  628.      `apply' returns the result of calling FUNCTION.  As with
  629.      `funcall', FUNCTION must either be a Lisp function or a primitive
  630.      function; special forms and macros do not make sense in `apply'.
  631.  
  632.           (setq f 'list)
  633.                => list
  634.           (apply f 'x 'y 'z)
  635.           error--> Wrong type argument: listp, z
  636.           (apply '+ 1 2 '(3 4))
  637.                => 10
  638.           (apply '+ '(1 2 3 4))
  639.                => 10
  640.           
  641.           (apply 'append '((a b c) nil (x y z) nil))
  642.                => (a b c x y z)
  643.  
  644.      For an interesting example of using `apply', see the description of
  645.      `mapcar', in *Note Mapping Functions::.
  646.  
  647.    It is common for Lisp functions to accept functions as arguments or
  648. find them in data structures (especially in hook variables and property
  649. lists) and call them using `funcall' or `apply'.  Functions that accept
  650. function arguments are often called "functionals".
  651.  
  652.    Sometimes, when you call a functional, it is useful to supply a no-op
  653. function as the argument.  Here are two different kinds of no-op
  654. function:
  655.  
  656.  - Function: identity ARG
  657.      This function returns ARG and has no side effects.
  658.  
  659.  - Function: ignore &rest ARGS
  660.      This function ignores any arguments and returns `nil'.
  661.  
  662. 
  663. File: lispref.info,  Node: Mapping Functions,  Next: Anonymous Functions,  Prev: Calling Functions,  Up: Functions
  664.  
  665. Mapping Functions
  666. =================
  667.  
  668.    A "mapping function" applies a given function to each element of a
  669. list or other collection.  XEmacs Lisp has three such functions;
  670. `mapcar' and `mapconcat', which scan a list, are described here.  For
  671. the third mapping function, `mapatoms', see *Note Creating Symbols::.
  672.  
  673.  - Function: mapcar FUNCTION SEQUENCE
  674.      `mapcar' applies FUNCTION to each element of SEQUENCE in turn, and
  675.      returns a list of the results.
  676.  
  677.      The argument SEQUENCE may be a list, a vector, or a string.  The
  678.      result is always a list.  The length of the result is the same as
  679.      the length of SEQUENCE.
  680.  
  681.      For example:
  682.  
  683.           (mapcar 'car '((a b) (c d) (e f)))
  684.                => (a c e)
  685.           (mapcar '1+ [1 2 3])
  686.                => (2 3 4)
  687.           (mapcar 'char-to-string "abc")
  688.                => ("a" "b" "c")
  689.  
  690.           ;; Call each function in `my-hooks'.
  691.           (mapcar 'funcall my-hooks)
  692.  
  693.           (defun mapcar* (f &rest args)
  694.             "Apply FUNCTION to successive cars of all ARGS.
  695.           Return the list of results."
  696.             ;; If no list is exhausted,
  697.             (if (not (memq 'nil args))
  698.                 ;; apply function to CARs.
  699.                 (cons (apply f (mapcar 'car args))
  700.                       (apply 'mapcar* f
  701.                              ;; Recurse for rest of elements.
  702.                              (mapcar 'cdr args)))))
  703.  
  704.           (mapcar* 'cons '(a b c) '(1 2 3 4))
  705.                => ((a . 1) (b . 2) (c . 3))
  706.  
  707.  - Function: mapconcat FUNCTION SEQUENCE SEPARATOR
  708.      `mapconcat' applies FUNCTION to each element of SEQUENCE: the
  709.      results, which must be strings, are concatenated.  Between each
  710.      pair of result strings, `mapconcat' inserts the string SEPARATOR.
  711.      Usually SEPARATOR contains a space or comma or other suitable
  712.      punctuation.
  713.  
  714.      The argument FUNCTION must be a function that can take one
  715.      argument and return a string.
  716.  
  717.           (mapconcat 'symbol-name
  718.                      '(The cat in the hat)
  719.                      " ")
  720.                => "The cat in the hat"
  721.  
  722.           (mapconcat (function (lambda (x) (format "%c" (1+ x))))
  723.                      "HAL-8000"
  724.                      "")
  725.                => "IBM.9111"
  726.  
  727. 
  728. File: lispref.info,  Node: Anonymous Functions,  Next: Function Cells,  Prev: Mapping Functions,  Up: Functions
  729.  
  730. Anonymous Functions
  731. ===================
  732.  
  733.    In Lisp, a function is a list that starts with `lambda', a byte-code
  734. function compiled from such a list, or alternatively a primitive
  735. subr-object; names are "extra".  Although usually functions are defined
  736. with `defun' and given names at the same time, it is occasionally more
  737. concise to use an explicit lambda expression--an anonymous function.
  738. Such a list is valid wherever a function name is.
  739.  
  740.    Any method of creating such a list makes a valid function.  Even
  741. this:
  742.  
  743.      (setq silly (append '(lambda (x)) (list (list '+ (* 3 4) 'x))))
  744.      => (lambda (x) (+ 12 x))
  745.  
  746. This computes a list that looks like `(lambda (x) (+ 12 x))' and makes
  747. it the value (*not* the function definition!) of `silly'.
  748.  
  749.    Here is how we might call this function:
  750.  
  751.      (funcall silly 1)
  752.      => 13
  753.  
  754. (It does *not* work to write `(silly 1)', because this function is not
  755. the *function definition* of `silly'.  We have not given `silly' any
  756. function definition, just a value as a variable.)
  757.  
  758.    Most of the time, anonymous functions are constants that appear in
  759. your program.  For example, you might want to pass one as an argument
  760. to the function `mapcar', which applies any given function to each
  761. element of a list.  Here we pass an anonymous function that multiplies
  762. a number by two:
  763.  
  764.      (defun double-each (list)
  765.        (mapcar '(lambda (x) (* 2 x)) list))
  766.      => double-each
  767.      (double-each '(2 11))
  768.      => (4 22)
  769.  
  770. In such cases, we usually use the special form `function' instead of
  771. simple quotation to quote the anonymous function.
  772.  
  773.  - Special Form: function FUNCTION-OBJECT
  774.      This special form returns FUNCTION-OBJECT without evaluating it.
  775.      In this, it is equivalent to `quote'.  However, it serves as a
  776.      note to the XEmacs Lisp compiler that FUNCTION-OBJECT is intended
  777.      to be used only as a function, and therefore can safely be
  778.      compiled.  Contrast this with `quote', in *Note Quoting::.
  779.  
  780.    Using `function' instead of `quote' makes a difference inside a
  781. function or macro that you are going to compile.  For example:
  782.  
  783.      (defun double-each (list)
  784.        (mapcar (function (lambda (x) (* 2 x))) list))
  785.      => double-each
  786.      (double-each '(2 11))
  787.      => (4 22)
  788.  
  789. If this definition of `double-each' is compiled, the anonymous function
  790. is compiled as well.  By contrast, in the previous definition where
  791. ordinary `quote' is used, the argument passed to `mapcar' is the
  792. precise list shown:
  793.  
  794.      (lambda (x) (* x 2))
  795.  
  796. The Lisp compiler cannot assume this list is a function, even though it
  797. looks like one, since it does not know what `mapcar' does with the
  798. list.  Perhaps `mapcar' will check that the CAR of the third element is
  799. the symbol `*'!  The advantage of `function' is that it tells the
  800. compiler to go ahead and compile the constant function.
  801.  
  802.    We sometimes write `function' instead of `quote' when quoting the
  803. name of a function, but this usage is just a sort of comment.
  804.  
  805.      (function SYMBOL) == (quote SYMBOL) == 'SYMBOL
  806.  
  807.    See `documentation' in *Note Accessing Documentation::, for a
  808. realistic example using `function' and an anonymous function.
  809.  
  810. 
  811. File: lispref.info,  Node: Function Cells,  Next: Inline Functions,  Prev: Anonymous Functions,  Up: Functions
  812.  
  813. Accessing Function Cell Contents
  814. ================================
  815.  
  816.    The "function definition" of a symbol is the object stored in the
  817. function cell of the symbol.  The functions described here access, test,
  818. and set the function cell of symbols.
  819.  
  820.    See also the function `indirect-function' in *Note Function
  821. Indirection::.
  822.  
  823.  - Function: symbol-function SYMBOL
  824.      This returns the object in the function cell of SYMBOL.  If the
  825.      symbol's function cell is void, a `void-function' error is
  826.      signaled.
  827.  
  828.      This function does not check that the returned object is a
  829.      legitimate function.
  830.  
  831.           (defun bar (n) (+ n 2))
  832.                => bar
  833.           (symbol-function 'bar)
  834.                => (lambda (n) (+ n 2))
  835.           (fset 'baz 'bar)
  836.                => bar
  837.           (symbol-function 'baz)
  838.                => bar
  839.  
  840.    If you have never given a symbol any function definition, we say that
  841. that symbol's function cell is "void".  In other words, the function
  842. cell does not have any Lisp object in it.  If you try to call such a
  843. symbol as a function, it signals a `void-function' error.
  844.  
  845.    Note that void is not the same as `nil' or the symbol `void'.  The
  846. symbols `nil' and `void' are Lisp objects, and can be stored into a
  847. function cell just as any other object can be (and they can be valid
  848. functions if you define them in turn with `defun').  A void function
  849. cell contains no object whatsoever.
  850.  
  851.    You can test the voidness of a symbol's function definition with
  852. `fboundp'.  After you have given a symbol a function definition, you
  853. can make it void once more using `fmakunbound'.
  854.  
  855.  - Function: fboundp SYMBOL
  856.      This function returns `t' if the symbol has an object in its
  857.      function cell, `nil' otherwise.  It does not check that the object
  858.      is a legitimate function.
  859.  
  860.  - Function: fmakunbound SYMBOL
  861.      This function makes SYMBOL's function cell void, so that a
  862.      subsequent attempt to access this cell will cause a `void-function'
  863.      error.  (See also `makunbound', in *Note Local Variables::.)
  864.  
  865.           (defun foo (x) x)
  866.                => x
  867.           (foo 1)
  868.                =>1
  869.           (fmakunbound 'foo)
  870.                => x
  871.           (foo 1)
  872.           error--> Symbol's function definition is void: foo
  873.  
  874.  - Function: fset SYMBOL OBJECT
  875.      This function stores OBJECT in the function cell of SYMBOL.  The
  876.      result is OBJECT.  Normally OBJECT should be a function or the
  877.      name of a function, but this is not checked.
  878.  
  879.      There are three normal uses of this function:
  880.  
  881.         * Copying one symbol's function definition to another.  (In
  882.           other words, making an alternate name for a function.)
  883.  
  884.         * Giving a symbol a function definition that is not a list and
  885.           therefore cannot be made with `defun'.  For example, you can
  886.           use `fset' to give a symbol `s1' a function definition which
  887.           is another symbol `s2'; then `s1' serves as an alias for
  888.           whatever definition `s2' presently has.
  889.  
  890.         * In constructs for defining or altering functions.  If `defun'
  891.           were not a primitive, it could be written in Lisp (as a
  892.           macro) using `fset'.
  893.  
  894.      Here are examples of the first two uses:
  895.  
  896.           ;; Give `first' the same definition `car' has.
  897.           (fset 'first (symbol-function 'car))
  898.                => #<subr car>
  899.           (first '(1 2 3))
  900.                => 1
  901.           
  902.           ;; Make the symbol `car' the function definition of `xfirst'.
  903.           (fset 'xfirst 'car)
  904.                => car
  905.           (xfirst '(1 2 3))
  906.                => 1
  907.           (symbol-function 'xfirst)
  908.                => car
  909.           (symbol-function (symbol-function 'xfirst))
  910.                => #<subr car>
  911.           
  912.           ;; Define a named keyboard macro.
  913.           (fset 'kill-two-lines "\^u2\^k")
  914.                => "\^u2\^k"
  915.  
  916.      See also the related functions `define-function' and `defalias',
  917.      in *Note Defining Functions::.
  918.  
  919.    When writing a function that extends a previously defined function,
  920. the following idiom is sometimes used:
  921.  
  922.      (fset 'old-foo (symbol-function 'foo))
  923.      (defun foo ()
  924.        "Just like old-foo, except more so."
  925.        (old-foo)
  926.        (more-so))
  927.  
  928. This does not work properly if `foo' has been defined to autoload.  In
  929. such a case, when `foo' calls `old-foo', Lisp attempts to define
  930. `old-foo' by loading a file.  Since this presumably defines `foo'
  931. rather than `old-foo', it does not produce the proper results.  The
  932. only way to avoid this problem is to make sure the file is loaded
  933. before moving aside the old definition of `foo'.
  934.  
  935.    But it is unmodular and unclean, in any case, for a Lisp file to
  936. redefine a function defined elsewhere.
  937.  
  938. 
  939. File: lispref.info,  Node: Inline Functions,  Next: Related Topics,  Prev: Function Cells,  Up: Functions
  940.  
  941. Inline Functions
  942. ================
  943.  
  944.    You can define an "inline function" by using `defsubst' instead of
  945. `defun'.  An inline function works just like an ordinary function
  946. except for one thing: when you compile a call to the function, the
  947. function's definition is open-coded into the caller.
  948.  
  949.    Making a function inline makes explicit calls run faster.  But it
  950. also has disadvantages.  For one thing, it reduces flexibility; if you
  951. change the definition of the function, calls already inlined still use
  952. the old definition until you recompile them.  Since the flexibility of
  953. redefining functions is an important feature of XEmacs, you should not
  954. make a function inline unless its speed is really crucial.
  955.  
  956.    Another disadvantage is that making a large function inline can
  957. increase the size of compiled code both in files and in memory.  Since
  958. the speed advantage of inline functions is greatest for small
  959. functions, you generally should not make large functions inline.
  960.  
  961.    It's possible to define a macro to expand into the same code that an
  962. inline function would execute.  But the macro would have a limitation:
  963. you can use it only explicitly--a macro cannot be called with `apply',
  964. `mapcar' and so on.  Also, it takes some work to convert an ordinary
  965. function into a macro.  (*Note Macros::.)  To convert it into an inline
  966. function is very easy; simply replace `defun' with `defsubst'.  Since
  967. each argument of an inline function is evaluated exactly once, you
  968. needn't worry about how many times the body uses the arguments, as you
  969. do for macros.  (*Note Argument Evaluation::.)
  970.  
  971.    Inline functions can be used and open-coded later on in the same
  972. file, following the definition, just like macros.
  973.  
  974. 
  975. File: lispref.info,  Node: Related Topics,  Prev: Inline Functions,  Up: Functions
  976.  
  977. Other Topics Related to Functions
  978. =================================
  979.  
  980.    Here is a table of several functions that do things related to
  981. function calling and function definitions.  They are documented
  982. elsewhere, but we provide cross references here.
  983.  
  984. `apply'
  985.      See *Note Calling Functions::.
  986.  
  987. `autoload'
  988.      See *Note Autoload::.
  989.  
  990. `call-interactively'
  991.      See *Note Interactive Call::.
  992.  
  993. `commandp'
  994.      See *Note Interactive Call::.
  995.  
  996. `documentation'
  997.      See *Note Accessing Documentation::.
  998.  
  999. `eval'
  1000.      See *Note Eval::.
  1001.  
  1002. `funcall'
  1003.      See *Note Calling Functions::.
  1004.  
  1005. `ignore'
  1006.      See *Note Calling Functions::.
  1007.  
  1008. `indirect-function'
  1009.      See *Note Function Indirection::.
  1010.  
  1011. `interactive'
  1012.      See *Note Using Interactive::.
  1013.  
  1014. `interactive-p'
  1015.      See *Note Interactive Call::.
  1016.  
  1017. `mapatoms'
  1018.      See *Note Creating Symbols::.
  1019.  
  1020. `mapcar'
  1021.      See *Note Mapping Functions::.
  1022.  
  1023. `mapconcat'
  1024.      See *Note Mapping Functions::.
  1025.  
  1026. `undefined'
  1027.      See *Note Key Lookup::.
  1028.  
  1029. 
  1030. File: lispref.info,  Node: Macros,  Next: Loading,  Prev: Functions,  Up: Top
  1031.  
  1032. Macros
  1033. ******
  1034.  
  1035.    "Macros" enable you to define new control constructs and other
  1036. language features.  A macro is defined much like a function, but instead
  1037. of telling how to compute a value, it tells how to compute another Lisp
  1038. expression which will in turn compute the value.  We call this
  1039. expression the "expansion" of the macro.
  1040.  
  1041.    Macros can do this because they operate on the unevaluated
  1042. expressions for the arguments, not on the argument values as functions
  1043. do.  They can therefore construct an expansion containing these
  1044. argument expressions or parts of them.
  1045.  
  1046.    If you are using a macro to do something an ordinary function could
  1047. do, just for the sake of speed, consider using an inline function
  1048. instead.  *Note Inline Functions::.
  1049.  
  1050. * Menu:
  1051.  
  1052. * Simple Macro::            A basic example.
  1053. * Expansion::               How, when and why macros are expanded.
  1054. * Compiling Macros::        How macros are expanded by the compiler.
  1055. * Defining Macros::         How to write a macro definition.
  1056. * Backquote::               Easier construction of list structure.
  1057. * Problems with Macros::    Don't evaluate the macro arguments too many times.
  1058.                               Don't hide the user's variables.
  1059.  
  1060. 
  1061. File: lispref.info,  Node: Simple Macro,  Next: Expansion,  Up: Macros
  1062.  
  1063. A Simple Example of a Macro
  1064. ===========================
  1065.  
  1066.    Suppose we would like to define a Lisp construct to increment a
  1067. variable value, much like the `++' operator in C.  We would like to
  1068. write `(inc x)' and have the effect of `(setq x (1+ x))'.  Here's a
  1069. macro definition that does the job:
  1070.  
  1071.      (defmacro inc (var)
  1072.         (list 'setq var (list '1+ var)))
  1073.  
  1074.    When this is called with `(inc x)', the argument `var' has the value
  1075. `x'--*not* the *value* of `x'.  The body of the macro uses this to
  1076. construct the expansion, which is `(setq x (1+ x))'.  Once the macro
  1077. definition returns this expansion, Lisp proceeds to evaluate it, thus
  1078. incrementing `x'.
  1079.  
  1080. 
  1081. File: lispref.info,  Node: Expansion,  Next: Compiling Macros,  Prev: Simple Macro,  Up: Macros
  1082.  
  1083. Expansion of a Macro Call
  1084. =========================
  1085.  
  1086.    A macro call looks just like a function call in that it is a list
  1087. which starts with the name of the macro.  The rest of the elements of
  1088. the list are the arguments of the macro.
  1089.  
  1090.    Evaluation of the macro call begins like evaluation of a function
  1091. call except for one crucial difference: the macro arguments are the
  1092. actual expressions appearing in the macro call.  They are not evaluated
  1093. before they are given to the macro definition.  By contrast, the
  1094. arguments of a function are results of evaluating the elements of the
  1095. function call list.
  1096.  
  1097.    Having obtained the arguments, Lisp invokes the macro definition just
  1098. as a function is invoked.  The argument variables of the macro are bound
  1099. to the argument values from the macro call, or to a list of them in the
  1100. case of a `&rest' argument.  And the macro body executes and returns
  1101. its value just as a function body does.
  1102.  
  1103.    The second crucial difference between macros and functions is that
  1104. the value returned by the macro body is not the value of the macro call.
  1105. Instead, it is an alternate expression for computing that value, also
  1106. known as the "expansion" of the macro.  The Lisp interpreter proceeds
  1107. to evaluate the expansion as soon as it comes back from the macro.
  1108.  
  1109.    Since the expansion is evaluated in the normal manner, it may contain
  1110. calls to other macros.  It may even be a call to the same macro, though
  1111. this is unusual.
  1112.  
  1113.    You can see the expansion of a given macro call by calling
  1114. `macroexpand'.
  1115.  
  1116.  - Function: macroexpand FORM &optional ENVIRONMENT
  1117.      This function expands FORM, if it is a macro call.  If the result
  1118.      is another macro call, it is expanded in turn, until something
  1119.      which is not a macro call results.  That is the value returned by
  1120.      `macroexpand'.  If FORM is not a macro call to begin with, it is
  1121.      returned as given.
  1122.  
  1123.      Note that `macroexpand' does not look at the subexpressions of
  1124.      FORM (although some macro definitions may do so).  Even if they
  1125.      are macro calls themselves, `macroexpand' does not expand them.
  1126.  
  1127.      The function `macroexpand' does not expand calls to inline
  1128.      functions.  Normally there is no need for that, since a call to an
  1129.      inline function is no harder to understand than a call to an
  1130.      ordinary function.
  1131.  
  1132.      If ENVIRONMENT is provided, it specifies an alist of macro
  1133.      definitions that shadow the currently defined macros.  Byte
  1134.      compilation uses this feature.
  1135.  
  1136.           (defmacro inc (var)
  1137.               (list 'setq var (list '1+ var)))
  1138.                => inc
  1139.  
  1140.           (macroexpand '(inc r))
  1141.                => (setq r (1+ r))
  1142.  
  1143.           (defmacro inc2 (var1 var2)
  1144.               (list 'progn (list 'inc var1) (list 'inc var2)))
  1145.                => inc2
  1146.  
  1147.           (macroexpand '(inc2 r s))
  1148.                => (progn (inc r) (inc s))  ; `inc' not expanded here.
  1149.  
  1150. 
  1151. File: lispref.info,  Node: Compiling Macros,  Next: Defining Macros,  Prev: Expansion,  Up: Macros
  1152.  
  1153. Macros and Byte Compilation
  1154. ===========================
  1155.  
  1156.    You might ask why we take the trouble to compute an expansion for a
  1157. macro and then evaluate the expansion.  Why not have the macro body
  1158. produce the desired results directly?  The reason has to do with
  1159. compilation.
  1160.  
  1161.    When a macro call appears in a Lisp program being compiled, the Lisp
  1162. compiler calls the macro definition just as the interpreter would, and
  1163. receives an expansion.  But instead of evaluating this expansion, it
  1164. compiles the expansion as if it had appeared directly in the program.
  1165. As a result, the compiled code produces the value and side effects
  1166. intended for the macro, but executes at full compiled speed.  This would
  1167. not work if the macro body computed the value and side effects
  1168. itself--they would be computed at compile time, which is not useful.
  1169.  
  1170.    In order for compilation of macro calls to work, the macros must be
  1171. defined in Lisp when the calls to them are compiled.  The compiler has a
  1172. special feature to help you do this: if a file being compiled contains a
  1173. `defmacro' form, the macro is defined temporarily for the rest of the
  1174. compilation of that file.  To use this feature, you must define the
  1175. macro in the same file where it is used and before its first use.
  1176.  
  1177.    Byte-compiling a file executes any `require' calls at top-level in
  1178. the file.  This is in case the file needs the required packages for
  1179. proper compilation.  One way to ensure that necessary macro definitions
  1180. are available during compilation is to require the files that define
  1181. them (*note Named Features::.).  To avoid loading the macro definition
  1182. files when someone *runs* the compiled program, write
  1183. `eval-when-compile' around the `require' calls (*note Eval During
  1184. Compile::.).
  1185.  
  1186. 
  1187. File: lispref.info,  Node: Defining Macros,  Next: Backquote,  Prev: Compiling Macros,  Up: Macros
  1188.  
  1189. Defining Macros
  1190. ===============
  1191.  
  1192.    A Lisp macro is a list whose CAR is `macro'.  Its CDR should be a
  1193. function; expansion of the macro works by applying the function (with
  1194. `apply') to the list of unevaluated argument-expressions from the macro
  1195. call.
  1196.  
  1197.    It is possible to use an anonymous Lisp macro just like an anonymous
  1198. function, but this is never done, because it does not make sense to pass
  1199. an anonymous macro to functionals such as `mapcar'.  In practice, all
  1200. Lisp macros have names, and they are usually defined with the special
  1201. form `defmacro'.
  1202.  
  1203.  - Special Form: defmacro NAME ARGUMENT-LIST BODY-FORMS...
  1204.      `defmacro' defines the symbol NAME as a macro that looks like this:
  1205.  
  1206.           (macro lambda ARGUMENT-LIST . BODY-FORMS)
  1207.  
  1208.      This macro object is stored in the function cell of NAME.  The
  1209.      value returned by evaluating the `defmacro' form is NAME, but
  1210.      usually we ignore this value.
  1211.  
  1212.      The shape and meaning of ARGUMENT-LIST is the same as in a
  1213.      function, and the keywords `&rest' and `&optional' may be used
  1214.      (*note Argument List::.).  Macros may have a documentation string,
  1215.      but any `interactive' declaration is ignored since macros cannot be
  1216.      called interactively.
  1217.  
  1218.